package com.prs.crm.action.receipt;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.receipt.ReceiptMoudleType;
import com.prs.crm.domain.receipt.ReceiptRegister;
import com.prs.crm.service.receipt.ReceiptMoudleTypeService;

public class ReceiptMoudleTypeAction  extends BaseAction{

	private static final long serialVersionUID = 1L;
	private ReceiptMoudleType receiptMoudleType;
	private ReceiptMoudleTypeService receiptMoudleTypeService;
	private Integer[] ids;
	private PaginationSupport pagination;
	public ReceiptMoudleType getReceiptMoudleType() {
		return receiptMoudleType;
	}
	public void setReceiptMoudleType(ReceiptMoudleType receiptMoudleType) {
		this.receiptMoudleType = receiptMoudleType;
	}
	public ReceiptMoudleTypeService getReceiptMoudleTypeService() {
		return receiptMoudleTypeService;
	}
	public void setReceiptMoudleTypeService(
			ReceiptMoudleTypeService receiptMoudleTypeService) {
		this.receiptMoudleTypeService = receiptMoudleTypeService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	@Action("listReceiptMoudleType")
	public String listReceiptMoudleType(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ReceiptMoudleType.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getReceiptMoudleTypeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editReceiptMoudleType")
	public String editReceiptMoudleType(){
		return SUCCESS;
	}
	
	@Action(value="saveReceiptMoudleType",results={
			@Result(name="success",type="redirect",location="listReceiptMoudleType"),
			@Result(name="error",type="chain",location="editReceiptMoudleType")
	})
	public String saveReceiptMoudleType(){
		if (receiptMoudleType.getName()==null || "".equals(receiptMoudleType.getName().trim())) {
			this.addActionError("模板名称不能为空！");
		}
		if (receiptMoudleType.getId()==null) {
			List<ReceiptMoudleType> re=this.getReceiptMoudleTypeService().getAllReceiptMoudleType();
			int r=0;
			for (ReceiptMoudleType res : re) {
				if (res.getName().equals(receiptMoudleType.getName())) {
					r++;
				}
			}
			if (r>0) {
				this.addActionError("模板名称不能重复！");
				return ERROR;
			}
			this.getReceiptMoudleTypeService().save(receiptMoudleType);
		}
		this.getReceiptMoudleTypeService().submit(receiptMoudleType);
		return SUCCESS;
	}
	@Action(value="removeReceiveMoudleType",results={
			@Result(name="success",type="redirect",location="listReceiptMoudleType"),
			@Result(name="error",type="chain",location="listReceiptMoudleType")
	})
	public String removeReceiveMoudleType(){
		if (ids==null) {
			this.addActionError("请选择一行！");
			return ERROR;
		}
		this.getReceiptMoudleTypeService().batchRemove(ids);
		return SUCCESS;
	}
}
